
$(function(){
    // console.log("wwww")
    $.ajax({
        url:'./php/course.php',
        type:'post',
        dataType:"json"
    }).done((result)=>{
        console.log(result);
        let strs=template("demo",{result});
        // console.log(result)
        $("#ct").html(strs);
        // console.log(strs) 
    })
})

//重加载
function reload(){
    $.ajax({
        url:'./php/course.php',
        type:'post',
        dataType:"json"
    }).done((result)=>{
        console.log(result);
        let strs=template("demo",{result});
        $("#ct").html(strs);

    })
}
   

// 查询课程的名称
function search(){
    toastr.options = {"timeOut": "5000"}
    // 查询课程的名称
    let f1=$("#f1")[0]//查询的表单
    let formData= new FormData(f1);
    $.ajax({
        // 查courseid
        url:"./php/searchCourse.php",
        method:"post",
        data:formData,
        dataType: "json",
        contentType: false,
        processData: false,
    }).done((data)=>{
        console.log(data)
        if(data){
              let strs=template("demo2",{data});
        // console.log(result)
        $("#ct").html("");//清空
        $("#ct").html(strs);
        }else{
            // alert("未搜索到该课程");
            toastr.warning("未搜索到该课程")
        }
      
    })
    let str= $("#word").val()//搜索框里的值
    console.log(str)
}




// 添加课程
function addCourse(){
    toastr.options = {"timeOut": "5000"}
    // console.log("wwww")
    let f2 = $("#f2")[0];
    let formData = new FormData(f2);
    $.ajax({
        // 查courseid
        url:"./php/addCourse.php",
        method:"post",
        data:formData,
        dataType: "json",
        contentType: false,
        processData: false,
    }).done((data)=>{
        if(data){
        toastr.success("课程添加成功");
        reload();//重加载
    }else{
        toastr.error("添加失败");
    }
    })
}

// 点击表格中更新默认显示
function doUpdate(x){
    // 
    var courseId=$(x).val();
    console.log(courseId)
    var obj={
        "courseId":courseId
    }
    $.ajax({
        url: "./php/updata.php",
        method: "post",
        data: obj,
        dataType: "json",
    }).done((result)=>{
        console.log("课程更新模态框：",result);
        $("#course1").val(result.courseId);
        $("#coursename1").val(result.courseName);
    });

}

// 更新完毕
function updated(){
    let cf1 = $("#cf1")[0];
    let formData= new FormData(cf1);
    $.ajax({
        url:"./php/updated.php",
        method:"post",
        data:formData,
        dataType: "json",
        contentType: false,
        processData: false,
    }).done((result) => {
        if(result.code==0){
            toastr.error(result.msg);
        }else if(result.code==1){
            toastr.error(result.msg);
        }else{
            toastr.success(result.msg);
            reload();
        }
    });
}

//禁用
function stop(obj){
    console.log(obj)
    var courseId=$(obj).val()//对应id
    console.log(courseId)
    let data={
        "courseId":courseId
    }
    console.log(data)
    $.ajax({
        url:"./php/stopCourse.php",
        type:"post",
        data:data,
        dataType:"json",
        success:function(result){
            console.log(result)
            reload();
            toastr.success("禁用成功");
            if(!result){
                toastr.error("禁用失败");
            }
        }
    })
}

//启用
function start(obj){
    var courseId=$(obj).val();
    let data={
    "courseId":courseId
   }
   console.log(data)
   $.ajax({
    url:"./php/courseStart.php",
    type:"post",
    data:data,
    dataType:"json",
    success:function(result){
        if(result){
            console.log(result);
            toastr.success("课程启用成功");
            reload();
        }else{
            toastr.error("课程启用失败");
        }
    }
});


}